<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增学期设置')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-term-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">学期：</label>
                <div class="col-sm-8">
<!--                    <input name="term" class="form-control" type="text">-->
                    <select id="term" name="term" class="form-control m-b">
                        <option value="1">第一学期</option>
                        <option value="2">第二学期</option>
                    </select>
                </div>



            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">开始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input id="startTime" name="startTime" class="form-control" placeholder="yyyy-MM-dd" type="text" onchange="isStartTime()" required>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input id="endTime" name="endTime" class="form-control" placeholder="yyyy-MM-dd" type="text" onchange="isEndTime()" required>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">学年：</label>
                <div class="col-sm-8">
                    <input id="schoolYear" name="schoolYear" class="form-control" type="text" placeholder="格式为xxxx-yyyy" style="" required>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script type="text/javascript">
        var prefix = ctx + "website/term"
        $("#form-term-add").validate({
            onkeyup: false,
            rules: {
                schoolYear: {
                    isSchoolYear : true,
                },
            },
            messages: {
                schoolYear: {
                    isSchoolYear : "请填写正确的时间格式"
                },
            },
            focusCleanup: true
        });

        function isStartTime(){
            setTimeout(function () {
                var startTime=$("#startTime").val();
                var endTime=$("#endTime").val();
                var term=$("#term").val();
                if(endTime !=''){
                    $("#schoolYear").val(startTime.substring(0,4)+"-"+endTime.substring(0,4));
                    var startTimes=$("#startTime").val();
                    var endTimes=$("#endTime").val();
                    if(term==1){
                        if(endTimes.substring(0,4)-startTimes.substring(0,4)!=1){
                            $("#schoolYear").val("")
                            $("#endTime").val("")
                            alert("第一学期需要跨一年！");
                        }
                    }
                }
            }, 400);
        }

        function isEndTime(){
            setTimeout(function () {
                var startTime=$("#startTime").val();
                var endTime=$("#endTime").val();
                var term=$("#term").val();
                if(startTime !=""){
                    $("#schoolYear").val(startTime.substring(0,4)+"-"+endTime.substring(0,4));
                    var startTimes=$("#startTime").val();
                    var endTimes=$("#endTime").val();
                    if(term==1) {
                        if (endTimes.substring(0, 4) - startTimes.substring(0, 4) != 1) {
                            $("#schoolYear").val("")
                            $("#endTime").val("")
                            alert("第一学期需要跨一年！");
                        }
                    }
                }
            }, 400);
        }

        function submitHandler() {
            if ($.validate.form()) {
                if($("#endTime").val()<=$("#startTime").val()){
                    $.modal.msgError("学期结束时间不能小于开始时间")
                }else{
                    /**
                     * 验证学年时间是否是日期时间
                     */
                    var school = $("#schoolYear").val();
                    var startTime=$("#startTime").val();
                    var endTime=$("#endTime").val();
                    if((startTime.substring(0,4)+"-"+endTime.substring(0,4))==school){
                        $.operate.save(prefix + "/add", $('#form-term-add').serialize());
                    }else{
                        $.modal.msgError("请不要任意更改学年")
                    }
                }
            }
        }

        $("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });


        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true,
        });
    </script>
</body>
</html>